<?php
/**
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 18/12/29
 * Time: 上午11:25
 */

namespace App\Helper;


use Illuminate\Support\Facades\Redis;

class BaseRedis
{

    protected static $conn = [];

    protected $connection;

    public function __construct($connection = 'default')
    {
        $this->connection = self::getInstance($connection);
    }

    public function getInstance($connection)
    {
        if(!isset(self::$conn) || !isset(self::$conn[$connection])){
            self::$conn[$connection] = Redis::connection($connection);
        }
        return self::$conn[$connection];
    }


    /**
     * --------------------------------------------------------------------------
     * 禁止外部克隆
     * --------------------------------------------------------------------------
     */
    private function __clone(){}


    public function del($key){
        return $this->connection->DEL($key);
    }

    public function exists($key){
        return $this->connection->EXISTS($key);
    }

    /**
     * --------------------------------------------------------------------------
     * 返回key的有效时间
     * --------------------------------------------------------------------------
     * @param $key
     * @return mixed
     */
    public function ttl($key){
        return $this->connection->TTL($key);
    }

    /**
     * --------------------------------------------------------------------------
     * 将字符串值 value 关联到 key
     * --------------------------------------------------------------------------
     * @param $key
     * @param $value
     * @param int $expire
     */
    public function set($key, $value, $expire = 0){
        $this->connection->SET($key, $value);
        if($expire) $this->connection->EXPIRE($key, $expire);
    }

    /**
     * --------------------------------------------------------------------------
     * 返回 key 所关联的字符串值。
     * 如果 key 不存在那么返回特殊值 nil
     * --------------------------------------------------------------------------
     * @param $key
     * @return mixed
     */
    public function get($key){
        return $this->connection->get($key);
    }

    public function incr($key){
        $this->connection->INCR($key);
    }


    // Hash（哈希表）
    /**
     * --------------------------------------------------------------------------
     * 同时将多个 field-value (域-值)对设置到哈希表 key 中
     * --------------------------------------------------------------------------
     * @param $key
     * @param $value
     * @param $expire
     */
    public function hmset($key, $value, $expire){
        $this->connection->HMSET($key, $value);
        if($expire) $this->connection->EXPIRE($key, $expire);
    }

    /**
     * --------------------------------------------------------------------------
     * 返回哈希表 key 中，所有的域和值
     * --------------------------------------------------------------------------
     * @param $key
     * @return mixed
     */
    public function hgetall($key){
        return $this->connection->HGETALL($key);
    }

    /**
     * --------------------------------------------------------------------------
     * 将哈希表 key 中的域 field 的值设为 value
     * --------------------------------------------------------------------------
     * @param $key
     */
    public function HSET($key)
    {
        $this->connection->HGET($key);
    }

    /**
     * --------------------------------------------------------------------------
     * 返回哈希表 key 中给定域 field 的值
     * --------------------------------------------------------------------------
     * @param $key
     * @param $field
     * @return mixed
     */
    public function hget($key, $field)
    {
        return $this->connection->HGET($key, $field);
    }

    // List（列表）
    /**
     * --------------------------------------------------------------------------
     * 将一个或多个值 value 插入到列表 key 的表尾(最右边)
     * --------------------------------------------------------------------------
     * @param $key
     * @param $value
     * @param int $expire
     */
    public function rpush($key, $value, $expire = 0)
    {
        $this->connection->RPUSH($key, $value);
        if($expire) $this->connection->EXPIRE($key, $expire);
    }

    /**
     * --------------------------------------------------------------------------
     * 移除并返回列表 key 的头元素
     * --------------------------------------------------------------------------
     * @param $key
     * @return mixed
     */
    public function lpop($key)
    {
        return $this->connection->LPOP($key);
    }

    /**
     * --------------------------------------------------------------------------
     * 返回列表 key 的长度
     * --------------------------------------------------------------------------
     * @param $key
     * @return mixed
     */
    public function llen($key){
        return $this->connection->LLEN($key);
    }

    // Set（集合）
    /**
     * --------------------------------------------------------------------------
     * 将一个或多个 member 元素加入到集合 key 当中，已经存在于集合的 member 元素将被忽略
     * --------------------------------------------------------------------------
     * @param $key
     * @param $value
     * @param int $expire
     */
    public function sadd($key, $value, $expire = 0)
    {
        $this->connection->SADD($key, $value);
        if($expire) $this->connection->EXPIRE($key, $expire);
    }


    /**
     * --------------------------------------------------------------------------
     * 返回集合 key 的基数(集合中元素的数量)
     * --------------------------------------------------------------------------
     * @param $key
     * @return mixed
     */
    public function scard($key)
    {
        return $this->connection->SCARD($key);
    }

    /**
     * --------------------------------------------------------------------------
     * 判断 member 元素是否集合 key 的成员
     * --------------------------------------------------------------------------
     * @param $key
     * @param $member
     * @return mixed
     */
    public function sismember($key, $member)
    {
        return $this->connection->SISMEMBER($key, $member);
    }


    /**
     * --------------------------------------------------------------------------
     * 返回集合 key 中的所有成员
     * --------------------------------------------------------------------------
     */
    public function smembers($key)
    {
        return $this->connection->SMEMBERS($key);
    }

    /**
     * --------------------------------------------------------------------------
     * 移除集合 key 中的一个或多个 member 元素，不存在的 member 元素会被忽略
     * --------------------------------------------------------------------------
     * @param $key
     * @param $member
     */
    public function srem($key, $member)
    {
        $this->connection->SREM($key, $member);
    }

    // SortedSet（有序集合）
    /**
     * --------------------------------------------------------------------------
     * 将一个或多个 member 元素及其 score 值加入到有序集 key 当中
     * --------------------------------------------------------------------------
     * @param $key
     * @param $score
     * @param $member
     * @param int $expire
     */
    public function zadd($key, $score, $member, $expire = 0 )
    {
        $this->connection->ZADD($key, $score, $member);
        if($expire) $this->connection->EXPIRE($key, $expire);
    }

    /**
     * --------------------------------------------------------------------------
     * 返回有序集 key 的基数
     * --------------------------------------------------------------------------
     * @param $key
     * @return mixed
     */
    public function zcard($key)
    {
        return $this->connection->ZCARD($key);
    }

}